package com.hitown.controller.train;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hitown.model.train.TrainRlyj;
import com.hitown.service.train.TrainRlyjService;
import com.hitown.util.Params;
import com.hitown.util.R;
import com.hitown.util.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Base64;
import java.util.Collection;

/**
 * <p>
 * 地铁公交人脸预警对象 前端控制器
 * </p>
 *
 * @author menu
 * @since 2021-08-09
 */
@RestController
@RequestMapping("/train-rlyj")
public class TrainRlyjController {

    @Autowired
    private TrainRlyjService rlyjService;

    @ApiOperation(value = "分页查询")
    @RequestMapping("selectPage")
    public R selectPage(@ApiIgnore @RequestBody Params<TrainRlyj> params){
        Page<TrainRlyj> page = new Page<>(params.getCurrent(),params.getSize());
        QueryWrapper<TrainRlyj> wrapper =new QueryWrapper<>();
        wrapper.select("ID","GAJGJGDM","DTXLDM","DTZDMC","GJXLDM","ZDRY_FLDM","ZDRY_XM","ZDRY_GMSFHM","YJSJ","CTU","to_char(CTM,'yyyy-MM-dd HH24:mi:ss') CTM","UTU","UTM","ZDRY_ZPRLZP","ZDRY_BKRLZP","ZDRY_ZPRLZP_DZ","ZDRY_BKRLZP_DZ");
        IPage<TrainRlyj> pageVo = rlyjService.page(page,wrapper);
        return R.ok().put("data",pageVo);

    }


    @ApiOperation(value = "根据id查询记录")
    @RequestMapping("getById")
    public R getById(@RequestParam String Id){
        TrainRlyj result = rlyjService.getById(Id);
        return R.ok().put("data",result);

    }

    @ApiOperation(value = "保存单条记录")
    @PostMapping("save")
    public R save(@RequestBody TrainRlyj entit){
        String zdryZprlzpBase64 = entit.getZdryZprlzpBase64();
        String zdrybkrlzpBase64 = entit.getZdryBkrlzpBase64();
        if(!StringUtils.isEmpty(zdryZprlzpBase64)){
            byte[] zdryZprlzp = Base64.getDecoder().decode(zdryZprlzpBase64);
            entit.setZdryZprlzp(zdryZprlzp);
            entit.setZdryZprlzpBase64(null);
        }
        if(!StringUtils.isEmpty(zdrybkrlzpBase64)){
            byte[] zdryBkrlzp = Base64.getDecoder().decode(zdrybkrlzpBase64);
            entit.setZdryZprlzp(zdryBkrlzp);
            entit.setZdryBkrlzpBase64(null);
        }
        Boolean result = rlyjService.save(entit);
        return R.ok().put("data",result);
    }


    @ApiOperation(value = "批量保存")
    @RequestMapping("saveBatch")
    public R saveBatch(@RequestBody Collection<TrainRlyj> entityList){
        for(TrainRlyj trainRlyj:entityList){
            String zdryZprlzpBase64 = trainRlyj.getZdryZprlzpBase64();
            String zdryBkrlzpBase64 = trainRlyj.getZdryBkrlzpBase64();
            if(!StringUtils.isEmpty(zdryZprlzpBase64)){
                byte[] zdryZprlzp = Base64.getDecoder().decode(zdryZprlzpBase64);
                trainRlyj.setZdryZprlzp(zdryZprlzp);
                trainRlyj.setZdryZprlzpBase64(null);
            }
            if(!StringUtils.isEmpty(zdryBkrlzpBase64)){
                byte[] zdryBkrlzp = Base64.getDecoder().decode(zdryBkrlzpBase64);
                trainRlyj.setZdryZprlzp(zdryBkrlzp);
                trainRlyj.setZdryBkrlzpBase64(null);
            }
        }
        Boolean result = rlyjService.saveBatch(entityList);
        return R.ok().put("data",result);

    }


    @ApiOperation(value = "根据 ID 选择修改")
    @RequestMapping("updateById")
    public R updateById(@RequestBody TrainRlyj entit){
        Boolean result = rlyjService.updateById(entit);
        return R.ok().put("data",result);

    }


    @ApiOperation(value = "根据ID 批量更新")
    @RequestMapping("updateBatchById")
    public R updateBatchById(@RequestBody Collection<TrainRlyj> entityList){
        Boolean result = rlyjService.updateBatchById(entityList);
        return R.ok().put("data",result);

    }


    @ApiOperation(value = "根据 ID 删除")
    @RequestMapping("removeById")
    public R removeById(@RequestParam String Id){
        Boolean result = rlyjService.removeById(Id);
        return R.ok().put("data",result);

    }


    @ApiOperation(value = "删除（根据ID 批量删除）")
    @RequestMapping("removeByIds")
    public R removeByIds(@RequestBody Collection<String> Ids){
        Boolean result = rlyjService.removeByIds(Ids);
        return R.ok().put("data",result);
    }
}

